<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>自定义服务（生成随机数）</title>
    <script src="../../js/plugins/angular/angular.min.js" type="text/javascript"></script>
    <script>

        var m1 = angular.module('myApp', []);


        m1.provider('myRandomNum', function () {
            return {
                bolInt: false,
                int: function (argBol) {
                    if (argBol) {
                        this.bolInt = true;
                    }
                    else {
                        this.bolInt = false;
                    }
                },
                $get: function () {
                    var This = this;
                    return function (num1, num2) {
                        return This.bolInt ? Math.round(Math.random() * (num2 - num1)) + num1 : Math.random() * (num2 - num1) + num1;
                    };
                }
            };
        });

        m1.config(['myRandomNumProvider', function (myRandomNumProvider) {
            // 为true代表整数，为false代表有小数
            myRandomNumProvider.int(true);

        }]);

        m1.controller('Aaa', ['$scope', 'myRandomNum', function ($scope, myRandomNum) {
            console.log(myRandomNum(-3, 6));
        }]);

    </script>
</head>

<body>
<div ng-controller="Aaa">

</div>
</body>
</html>
